package com.qf.studentmanager.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.studentmanager.model.Course;
import com.qf.studentmanager.model.RespBean;
import com.qf.studentmanager.service.CourseService;
import com.qf.studentmanager.service.UserService;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/CourseListServlet")
public class CourseListServlet extends HttpServlet {
    CourseService courseService = new CourseService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
//        CourseService courseService = new CourseService();
        RespBean respBean = null;
        Course course = objectMapper.readValue(request.getInputStream(), Course.class);
        Integer integer = courseService.addCourse(course);
        if (integer== 1){
            respBean = RespBean.ok("添加成功");

        }else if (integer == -2){
            respBean = RespBean.error("课程重复");
        }else {
            respBean = RespBean.error("添加失败");
        }
        String string = objectMapper.writeValueAsString(respBean);
        response.getWriter().write(string);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        UserService userService = new UserService();
        ObjectMapper objectMapper = new ObjectMapper();
        response.setContentType("application/json;charset=utf-8");
        if ("page".equals(action)){
            request.getRequestDispatcher("WEB-INF/jsp/course/courseList.jsp").forward(request, response);
        }else if ("CourseList".equals(action)){
                List<Course> list = userService.getCourses();
                String string = objectMapper.writeValueAsString(list);
                response.getWriter().write(string);
        }else if ("combobox".equals(action)){
            String gid = request.getParameter("gid");
            List<Course> courses = courseService.getCoursesByGid(gid);
            String string = objectMapper.writeValueAsString(courses);
            response.getWriter().write(string);
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        String cid = req.getParameter("cid");
        Integer integer = courseService.deleteCourse(Integer.parseInt(cid));
        ObjectMapper objectMapper = new ObjectMapper();
        RespBean respBean = null;
        if (integer == 1){
            respBean= RespBean.ok("删除成功");
            String string = objectMapper.writeValueAsString(respBean);
            resp.getWriter().write(string);
        }else {  //要考虑 用else还是trycatch，不过有可能异常，就用try，不可能有异常就用else，比如在service层捕获并处理了异常，servlet就不可能有异常，就只能用else。
            respBean=RespBean.error("删除失败");
            String string = objectMapper.writeValueAsString(respBean);
            resp.getWriter().write(string);
        }
    }
}
